package org.lzx.converter.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lzx.converter.file.GenerateClassStructure;

/**
 * 
 * @author swagner
 * @date 29.06.2008
 *
 */
public class ReplaceString {
	
	private static final Log log = LogFactory.getLog(ReplaceString.class);
	
	private static ReplaceString instance;

	private ReplaceString() {}

	public static synchronized ReplaceString getInstance() {
		if (instance == null) {
			instance = new ReplaceString();
		}
		return instance;
	}
	
	public void processFile(File inputFile, File outputFile) {
		try {
//			String regex = "new drawViewNew";
//			String replacement = "new lz.drawViewNew";
			
			log.debug("Process: "+inputFile);
			
			this.doReplaceNewClassInititialization(inputFile, outputFile);
		} catch (Exception err) {
			log.error("[processFile]",err);
		}
	}
	
	public void doReplaceNewClassInititialization(File inputFile, File outputDir) throws Exception{
		
			
		    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
		    BufferedWriter writer = new BufferedWriter(new FileWriter(outputDir));
		 
		    boolean searchForCloseEventMethodTag = false;
		    int i = 0;
		    
		    while (true) {
		        String line = reader.readLine();
		        i++;
		 
		        if (line == null) {
		            break;
		        }
		 
		        for (Iterator<String> iter = GenerateClassStructure.currentClassNameList.iterator();iter.hasNext();){
		        	String className = iter.next();
		        	String regex = "new "+className;
		        	String replacement = "new lz."+className;
		        	line = line.replaceAll(regex, replacement);
		        	
		        	String regex_2 = "instanceof "+className;
		        	String replacement_2 = "instanceof lz."+className;
		        	line = line.replaceAll(regex_2, replacement_2);
		        }
		        
		        String regex2 = "<method event";
		        String replacement2 = "<handler name";
		        String[] replaceArray = line.split(regex2);
		        //log.debug("replaceArray.length: "+replaceArray.length);
		        if (replaceArray.length>1){
		        	log.debug("Found Open Handler Line: "+i);
		        	searchForCloseEventMethodTag = true;
		        	line = line.replaceAll(regex2, replacement2);
		        }
		        
		        if (searchForCloseEventMethodTag) {
		        	String regex3 = "method";
			        String replacement3 = "handler";
			        String[] replaceArrayCloseTag = line.split(regex3);
			        if (replaceArrayCloseTag.length>1){
			        	log.debug("Found Close Handler Line: "+i);
			        	searchForCloseEventMethodTag = false;
			        	line = line.replaceAll(regex3, replacement3);
			        }
		        }
		        
	        	String regex4 = "new global";
	        	String replacement4 = "new lz";
	        	line = line.replaceAll(regex4, replacement4);
	        	
	        	
		        writer.write(line);
		        writer.newLine();
		    }
		 
		    writer.close();
		    reader.close();
		
	}
	
}
